/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.forum.web.extension;

import net.sourceforge.stripes.config.ConfigurableComponent;
import net.sourceforge.stripes.config.RuntimeConfiguration;
import net.sourceforge.stripes.controller.Interceptor;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringHelper;

import java.util.Collection;
import java.util.Map;

/**
 * This Stripes extension allows
 * <ul>
 *  <li>{@link net.sourceforge.stripes.controller.Interceptor}</li>
 *  <li>{@link net.sourceforge.stripes.config.ConfigurableComponent} (basically all resolvers and factories)</li>
 * </ul>
 * to be Spring object injectable. ie. @SpringBean annotation could be used in them to allow them to be
 * injected with dependencies.
 *
 * @author tmjee
 * @version $Date$ $Id$
 */
public class SpringEnabledRuntimeConfiguration extends RuntimeConfiguration {

    protected void addInterceptor(Map<LifecycleStage, Collection<Interceptor>> map, Interceptor interceptor) {
        super.addInterceptor(map, interceptor);
        if (interceptor != null) {
            SpringHelper.injectBeans(interceptor, getBootstrapPropertyResolver().getFilterConfig().getServletContext());
        }
    }

    @SuppressWarnings("unchecked")
    protected <T extends ConfigurableComponent> T initializeComponent(Class<T> componentType, String propertyName) {
        ConfigurableComponent configurableComponent = super.initializeComponent(componentType, propertyName);
        if (configurableComponent != null) {
            SpringHelper.injectBeans(configurableComponent, getBootstrapPropertyResolver().getFilterConfig().getServletContext());
        }
        return (T) configurableComponent;
    }
}
